package xin.marcher.module.price.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.enums.construction.AuditCustomTypeEnum;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.enums.seller.SellerTypeEnum;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.price.domain.dto.AuditContentConfigDTO;
import xin.marcher.module.price.domain.dto.AuditContentConfigResultDTO;
import xin.marcher.module.price.domain.request.AuditContentConfigRequest;
import xin.marcher.module.price.repository.AuditRepository;
import xin.marcher.module.price.service.AuditContentConfigService;

import java.util.List;
import java.util.Objects;

import static xin.marcher.module.common.constants.price.AuditContentConfigConstants.QUERY_AUDIT_CONTENT_MAX_COUNT;

/**
 *
 */
@Service
public class AuditContentConfigServiceImpl implements AuditContentConfigService {

    @Autowired
    private AuditRepository auditRepository;

    /**
     * 创建审核内容配置
     *
     * @param request
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuditContentConfigResultDTO saveAuditContentConfig(AuditContentConfigRequest request) {
        // 检查入参
        checkSaveParam(request);
        // 新增审核配置
        auditRepository.saveAuditContentConfig(request);
        return new AuditContentConfigResultDTO(Boolean.TRUE);
    }

    /**
     * 删除审核内容配置
     *
     * @param customIdList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuditContentConfigResultDTO removeAuditContentConfig(List<String> customIdList) {
        auditRepository.removeAuditContentConfig(customIdList);
        return new AuditContentConfigResultDTO(Boolean.TRUE);
    }

    /**
     * 查询审核内容配置
     *
     * @param customIdList
     * @return
     */
    @Override
    public List<AuditContentConfigDTO> queryAuditContentConfig(List<String> customIdList) {
        // 参数检查
        checkQueryParam(customIdList);
        // 分批次查询
        return auditRepository.queryAuditContentConfig(customIdList);
    }

    /**
     * 创建审核内容入参检查
     *
     * @param request
     */
    private void checkSaveParam(AuditContentConfigRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StringUtils.isEmpty(request.getCustomId())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(AuditCustomTypeEnum.getByCode(request.getCustomType()))) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (!Objects.equals(SellerTypeEnum.POP.getCode(), request.getSellerType()) &&
                !Objects.equals(SellerTypeEnum.SELF.getCode(), request.getSellerType())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }

    /**
     * 查询审核内容参数检查
     *
     * @param customIdList
     */
    private void checkQueryParam(List<String> customIdList) {
        if (CollectionUtils.isEmpty(customIdList)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
        if (customIdList.size() > QUERY_AUDIT_CONTENT_MAX_COUNT) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
